<template>
    <div id="tree">
        <el-button-group v-if="$attrs.pageType != 'source-maintenance'">
            <el-button :class="{ active: activeItem === 'source' }" @click="handleBtnClick('source')">来源</el-button>
            <el-button :class="{ active: activeItem === 'fund' }" @click="handleBtnClick('fund')">产品</el-button>
            <el-button :class="{ active: activeItem === 'entryType' }" @click="handleBtnClick('entryType')" v-if="!forProduction">条目类型</el-button>
        </el-button-group>
        <el-input v-model="keyword" :placeholder="placeholder" size="mini">
            <i slot="suffix" class="el-input__icon el-icon-search" style="cursor: pointer" @click="selectByKeyword"></i>
        </el-input>
        <!-- 来源树 -->
        <yh-tree
            ref="source"
            :data="treeData"
            node-key="id"
            :current-node-key="currentNodeKey"
            default-expand-all
            :default-expanded-keys="expandedKeys"
            highlight-current
            :expand-on-click-node="false"
            @node-click="handleNodeClick"
        >
            <span class="custom-tree-node" slot-scope="{ node }">
                <span>
                    <i v-if="node.level === 1 || (node.level == 2 && $attrs.activeItem != 'entry-type')" class="el-icon-office-building"></i>
                    <i v-else-if="node.level === 3 && $attrs.activeItem != 'entry-type'" class="el-icon-collection"></i>
                    {{ node.label }}
                </span>
            </span>
        </yh-tree>
    </div>
</template>
<script lang="ts">
import { Component, Vue, Prop } from 'vue-property-decorator';
import API from '../../api/index';
import { ZCarrayToTree } from '@/utils/tools';
import { SourceTreeVO } from '../../vo/index';

@Component({ name: 'CardTree', components: {} })
export default class CardTree extends Vue {
    activeItem: string = 'source'; // fund
    keyword: string = '';
    treeData: any[] = []; // 来源树数据
    expandedKeys: string[] = ['1', '2'];
    currentNodeKey: string = '1';
    currentNode: any = null;
    sourceData: any[] = []; // 所有来源文档
    fundData: any[] = []; // 产品数据

    async mounted() {
        // this.sourceData = await API.getSourceData();
        this.getTreeData();
    }

    get placeholder() {
        if (this.activeItem == 'source') {
            return '请输入来源类型/分组/名称';
        } else if (this.activeItem == 'fund') {
            return '请输入产品类型/分组/名称';
        } else if (this.activeItem == 'entryType') {
            return '请输入YH条目类型';
        }
    }

    /**
     * 是否为合规卡制作
     */
    get forProduction() {
        return this.$attrs.pageType == 'production';
    }

    getTreeData() {
        API.getSourceTree({ articleType: '2' }).then((res: any[]) => {
            res = [
                {
                    articleType: '2',
                    createDate: 20231102,
                    createTime: 150000,
                    createUser: 'administrator',
                    currentValidNodeNum: 0,
                    id: '1',
                    isProduct: false,
                    label: '全部组合',
                    level: 1,
                    modifyDate: 20231102,
                    modifyTime: 150000,
                    modifyUser: 'administrator',
                    parentId: ' ',
                    path: '全部组合',
                    systemData: true,
                },
                {
                    articleType: '2',
                    createDate: 20231208,
                    createTime: 152047,
                    createUser: '1130',
                    currentValidNodeNum: 0,
                    id: '147',
                    isProduct: false,
                    label: '大账户',
                    level: 2,
                    modifyDate: 20231208,
                    modifyTime: 152047,
                    modifyUser: '1130',
                    parentId: '1',
                    path: '全部组合-大账户',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231208,
                    createTime: 152528,
                    createUser: '20',
                    currentValidNodeNum: 0,
                    fdProductCode: 'GF43418',
                    id: '150',
                    isProduct: true,
                    label: '赢和111号简称',
                    level: 3,
                    modifyDate: 20231208,
                    modifyTime: 152528,
                    modifyUser: '20',
                    parentId: '147',
                    path: '全部组合-大账户-赢和111号简称',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231208,
                    createTime: 153432,
                    createUser: '20',
                    currentValidNodeNum: 0,
                    id: '151',
                    isProduct: false,
                    label: '产品管理-测试数据（勿动）',
                    level: 2,
                    modifyDate: 20231208,
                    modifyTime: 153432,
                    modifyUser: '20',
                    parentId: '1',
                    path: '全部组合-产品管理-测试数据（勿动）',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231211,
                    createTime: 145645,
                    createUser: '20',
                    currentValidNodeNum: 0,
                    id: '157',
                    isProduct: false,
                    label: '合同新增',
                    level: 2,
                    modifyDate: 20231211,
                    modifyTime: 145645,
                    modifyUser: '20',
                    parentId: '1',
                    path: '全部组合-合同新增',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231211,
                    createTime: 151315,
                    createUser: '20',
                    currentValidNodeNum: 0,
                    id: '160',
                    isProduct: false,
                    label: '新增三级菜单目录验证',
                    level: 3,
                    modifyDate: 20231211,
                    modifyTime: 151315,
                    modifyUser: '20',
                    parentId: '151',
                    path: '全部组合-产品管理-测试数据（勿动）-新增三级菜单目录验证',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231211,
                    createTime: 151333,
                    createUser: '20',
                    currentValidNodeNum: 0,
                    id: '161',
                    isProduct: false,
                    label: '新增四级菜单目录验证',
                    level: 4,
                    modifyDate: 20231211,
                    modifyTime: 151333,
                    modifyUser: '20',
                    parentId: '160',
                    path: '全部组合-产品管理-测试数据（勿动）-新增三级菜单目录验证-新增四级菜单目录验证',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231212,
                    createTime: 192425,
                    createUser: '20',
                    currentValidNodeNum: 0,
                    id: '166',
                    isProduct: false,
                    label: '新增菜单验证条款条目管理（勿动）',
                    level: 2,
                    modifyDate: 20231212,
                    modifyTime: 192425,
                    modifyUser: '20',
                    parentId: '1',
                    path: '全部组合-新增菜单验证条款条目管理（勿动）',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231213,
                    createTime: 184156,
                    createUser: '20',
                    currentValidNodeNum: 0,
                    id: '171',
                    isProduct: false,
                    label: '监管类合同（测试待办铃铛）',
                    level: 2,
                    modifyDate: 20231213,
                    modifyTime: 184156,
                    modifyUser: '20',
                    parentId: '1',
                    path: '全部组合-监管类合同（测试待办铃铛）',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231214,
                    createTime: 143803,
                    createUser: '20',
                    currentValidNodeNum: 0,
                    id: '174',
                    isProduct: false,
                    label: '新增四级菜单验证禅道外规内化bug（勿动）',
                    level: 3,
                    modifyDate: 20231214,
                    modifyTime: 143803,
                    modifyUser: '20',
                    parentId: '166',
                    path: '全部组合-新增菜单验证条款条目管理（勿动）-新增四级菜单验证禅道外规内化bug（勿动）',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231214,
                    createTime: 195818,
                    createUser: '1130',
                    currentValidNodeNum: 0,
                    id: '178',
                    isProduct: false,
                    label: '第二个四级',
                    level: 4,
                    modifyDate: 20231214,
                    modifyTime: 195818,
                    modifyUser: '1130',
                    parentId: '160',
                    path: '全部组合-产品管理-测试数据（勿动）-新增三级菜单目录验证-第二个四级',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231215,
                    createTime: 104627,
                    createUser: '8888',
                    currentValidNodeNum: 0,
                    id: '180',
                    isProduct: false,
                    label: '新增四级菜单验证禅道bug(勿动)',
                    level: 4,
                    modifyDate: 20231215,
                    modifyTime: 104627,
                    modifyUser: '8888',
                    parentId: '174',
                    path: '全部组合-新增菜单验证条款条目管理（勿动）-新增四级菜单验证禅道外规内化bug（勿动）-新增四级菜单验证禅道bug(勿动)',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231215,
                    createTime: 142634,
                    createUser: '20',
                    currentValidNodeNum: 0,
                    id: '184',
                    isProduct: false,
                    label: '制度类合同（测试静态报告查询制度类型条件）',
                    level: 2,
                    modifyDate: 20231215,
                    modifyTime: 142634,
                    modifyUser: '20',
                    parentId: '1',
                    path: '全部组合-制度类合同（测试静态报告查询制度类型条件）',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231215,
                    createTime: 143034,
                    createUser: '1130',
                    currentValidNodeNum: 0,
                    fdProductCode: 'GF100538',
                    id: '185',
                    isProduct: true,
                    label: '赢和627号简称',
                    level: 3,
                    modifyDate: 20231215,
                    modifyTime: 143034,
                    modifyUser: '1130',
                    parentId: '184',
                    path: '全部组合-制度类合同（测试静态报告查询制度类型条件）-赢和627号简称',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231215,
                    createTime: 161108,
                    createUser: '20',
                    currentValidNodeNum: 0,
                    id: '188',
                    isProduct: false,
                    label: '公司制度解读类（测试待办铃铛产品合同新增）',
                    level: 2,
                    modifyDate: 20231215,
                    modifyTime: 161108,
                    modifyUser: '20',
                    parentId: '1',
                    path: '全部组合-公司制度解读类（测试待办铃铛产品合同新增）',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231218,
                    createTime: 105422,
                    createUser: '20',
                    currentValidNodeNum: 0,
                    id: '190',
                    isProduct: false,
                    label: '新增四级菜单_002_并发',
                    level: 4,
                    modifyDate: 20231218,
                    modifyTime: 105422,
                    modifyUser: '20',
                    parentId: '174',
                    path: '全部组合-新增菜单验证条款条目管理（勿动）-新增四级菜单验证禅道外规内化bug（勿动）-新增四级菜单_002_并发',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231218,
                    createTime: 110804,
                    createUser: '20',
                    currentValidNodeNum: 0,
                    id: '194',
                    isProduct: false,
                    label: '新增菜单_验证禅道bug_1218',
                    level: 4,
                    modifyDate: 20231218,
                    modifyTime: 110804,
                    modifyUser: '20',
                    parentId: '174',
                    path: '全部组合-新增菜单验证条款条目管理（勿动）-新增四级菜单验证禅道外规内化bug（勿动）-新增菜单_验证禅道bug_1218',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231220,
                    createTime: 150631,
                    createUser: '20',
                    currentValidNodeNum: 0,
                    id: '207',
                    isProduct: false,
                    label: '【待办铃铛】admin新建的产品合同（出闲了修改）',
                    level: 2,
                    modifyDate: 20231220,
                    modifyTime: 150631,
                    modifyUser: '20',
                    parentId: '1',
                    path: '全部组合-【待办铃铛】admin新建的产品合同（出闲了修改）',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20231225,
                    createTime: 140746,
                    createUser: '20',
                    currentValidNodeNum: 0,
                    id: '209',
                    isProduct: false,
                    label: '新增合同菜单_1225(编写案例场景)',
                    level: 3,
                    modifyDate: 20231225,
                    modifyTime: 140746,
                    modifyUser: '20',
                    parentId: '166',
                    path: '全部组合-新增菜单验证条款条目管理（勿动）',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20240110,
                    createTime: 161302,
                    createUser: '1130',
                    currentValidNodeNum: 0,
                    id: '240',
                    isProduct: false,
                    label: 'BUG-产品类型无当前层级22',
                    level: 4,
                    modifyDate: 20240110,
                    modifyTime: 161302,
                    modifyUser: '1130',
                    parentId: '147',
                    path: '全部组合-大账户-BUG-产品类型无当前层级22',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20240110,
                    createTime: 161933,
                    createUser: '1130',
                    currentValidNodeNum: 0,
                    id: '241',
                    isProduct: false,
                    label: 'BUG22-产品类型无当前层级',
                    level: 4,
                    modifyDate: 20240110,
                    modifyTime: 161933,
                    modifyUser: '1130',
                    parentId: '147',
                    path: '全部组合-大账户-BUG22-产品类型无当前层级',
                    systemData: false,
                },
                {
                    articleType: '2',
                    createDate: 20240112,
                    createTime: 213916,
                    createUser: '20',
                    currentValidNodeNum: 0,
                    fdProductCode: 'GF100946',
                    id: '268',
                    isProduct: true,
                    label: '赢和184号简称',
                    level: 3,
                    modifyDate: 20240112,
                    modifyTime: 213916,
                    modifyUser: '20',
                    parentId: '267',
                    path: '全部非标-非标-赢和184号简称',
                    systemData: false,
                },
            ];
            if (this.activeItem === 'source') {
                res = ZCarrayToTree(res);
                this.treeData = res;
            }
            if (this.activeItem === 'fund') {
                // 根据 isProduct 拆出 具体的产品
                this.fundData = res.filter(i => i.isProduct);
                console.log(this.fundData);
                res = ZCarrayToTree(res);
                this.treeData = res;
            }
            console.log(this.treeData, 'treeData');
            this.nodeStyleSetting();
        });
    }

    /**
     * 根据关键字查询
     */
    selectByKeyword() {}

    /**
     * 切换 来源/产品树 事件
     * @param 来源/产品 source/fund
     */
    handleBtnClick(param: string): void {
        this.activeItem = param;
        this.getTreeData();
        this.$emit('handleActiveItem', param);
    }

    /**
     * 点击节点
     */
    handleNodeClick(data, node): void {
        console.log('树节点切换:', data);
        this.currentNode = data;
        this.$emit('handleTreeNodeData', data);
    }

    /**
     * 回显展示节点和选中节点
     */
    nodeStyleSetting(): void {
        if (this.activeItem === 'source') {
            if (this.currentNode) {
                this.currentNodeKey = this.currentNode.id;
                this.$nextTick(() => {
                    (this.$refs['source'] as any).setCurrentKey(this.currentNode.id);
                });
            }
        }
    }

    /**
     * 在树结构中查找某个节点， 并返回与之关联的节点
     */
    findNode(tree, targetId, result = []) {
        for (let i = 0; i < tree.length; i++) {
            const { id, children } = tree[i];
            if (id === targetId) {
                return result.concat(tree[i]);
            } else if (children) {
                const res = this.findNode(children, targetId, result.concat(tree[i]));
                if (res) {
                    return res;
                }
            }
        }
        return null;
    }

    /**
     * 对象数组 根据对象内的某个属性去重
     */
    uniqueById(arr) {
        const map = {};
        const result = [];
        for (let i = 0; i < arr.length; i++) {
            const item = arr[i];
            if (!map[item.id]) {
                map[item.id] = true;
                delete item.children;
                result.push(item);
            }
        }
        return result;
    }
}
</script>
<style lang="scss" scoped>
#tree {
    height: 100%;

    .el-button-group {
        width: 100%;
        display: flex;

        .el-button {
            flex: 1;
            padding: 0;
            height: 30px;
            line-height: 30px;

            /deep/ span {
                width: 90%;
            }
        }

        .active {
            color: var(--primary);
            border-color: var(--primary);
        }
    }

    /deep/ .el-tree {
        // 高度在来源管理中-30px
        height: calc(100% - 60px);
        overflow: auto;

        .el-tree-node__content {
            &:hover {
                background: #aaaaaa;
                border-radius: 5px;
                color: #575757;
            }
            .custom-tree-node span i {
                font-size: 16px;
                color: var(--primary);
            }
        }
    }
}

/deep/ .el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
    background: var(--primary);
    border-radius: 5px;

    .custom-tree-node {
        color: #fff;
        font-weight: bold;
        span i {
            color: #fff !important;
        }
    }
}

/deep/ .el-form-item.is-required:not(.is-no-asterisk) > .el-form-item__label:before {
    content: '*' !important;
    margin-right: 2px !important;
    color: red;
}

/deep/ .el-form-item.is-required:not(.is-no-asterisk) > .el-form-item__label:after {
    display: none;
}

/deep/ .el-dialog {
    .el-dialog__header {
        background: #f9f9f9;

        .el-dialog__title,
        .el-dialog__headerbtn .el-dialog__close {
            color: #333333;
        }
    }

    .el-dialog__body .el-form-item__label {
        height: 30px;
        /* vertical-align: bottom; */
        overflow: visible;
        padding: 0;
        font-size: 12px;
    }

    .el-dialog__footer {
        text-align: right;
        border-top: none;
        .el-button {
            min-width: 50px;
            border-radius: 4px;
        }
        .el-button + .el-button {
            margin-left: 10px;
        }

        .dialog-footer {
            padding: 0 10px;
        }
    }
}

.el-button-group > .el-button + .el-button {
    margin-left: 2px;
}
</style>
